/**
 * Created by zhangsl
 */
import Info from "./Info";

export default class Base {
  public static readonly Info: Info = new Info();

  public sleep(time: number) {
    return new Promise((resolve, reject) => {
      setTimeout(resolve, time);
    });
  }

  public dateFormat(date: Date, format: string) {
    const option: any = {
      "M+": date.getMonth() + 1,
      "d+": date.getDate(),
      "h+": date.getHours(),
      "m+": date.getMinutes(),
      "s+": date.getSeconds(),
      "q+": Math.floor((date.getMonth() + 3) / 3),
      "S": date.getMilliseconds(),
    };
    if (/(y+)/.test(format)) {
      format = format.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    for (const key in option) {
      if (new RegExp(`(${key})`).test(format)) {
        const content = (RegExp.$1.length === 1) ? (option[key]) :
          (("00" + option[key]).substr(("" + option[key]).length));
        format        = format.replace(RegExp.$1, content);
      }
    }
    return format;
  }
}
